package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.DetailPurchaseEntity;
import com.softlab.retailwholesalesystem.entity.PurchaseEntity;
import com.softlab.retailwholesalesystem.entity.VO.PurchaseGoodsVOEntity;
import com.softlab.retailwholesalesystem.mapper.DetailPurchaseMapper;
import com.softlab.retailwholesalesystem.service.IDetailPurchaseService;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import com.softlab.retailwholesalesystem.service.IRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DetailPurchaseServiceImpl extends ServiceImpl<DetailPurchaseMapper, DetailPurchaseEntity> implements IDetailPurchaseService {
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IRepositoryService repositoryService;

    @Autowired
    private DetailPurchaseMapper mapper;

    @Override
    public List<PurchaseGoodsVOEntity> getMap(Long id) {
        QueryWrapper<DetailPurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_id",id);
        wrapper.eq("is_delete",0);
        List<DetailPurchaseEntity> entities = mapper.selectList(wrapper);
        List<PurchaseGoodsVOEntity> res = new ArrayList<>();
        for (DetailPurchaseEntity entity:
                entities) {
            res.add(transfer(entity));
        }


        return res;
    }

    @Override
    public PurchaseGoodsVOEntity transfer(DetailPurchaseEntity entity) {
        PurchaseGoodsVOEntity goodsVO = new PurchaseGoodsVOEntity(entity);
        goodsVO.setGoodsName(goodsService.getById(entity.getGoodsId()).getGoodsName());
        goodsVO.setRepositoryName(repositoryService.getById(entity.getRepositoryId()).getRepositoryName());
        return goodsVO;
    }

    @Override
    public List<PurchaseGoodsVOEntity> search(PurchaseEntity entity) {
        QueryWrapper<DetailPurchaseEntity> wrapper = new QueryWrapper<>();
        if (entity.getStatus() != null) wrapper.eq("status", entity.getStatus());
        if (entity.getName() != null) wrapper.eq("name", entity.getName());
        if (entity.getPurchaseId() != null) wrapper.eq("purchase_id", entity.getPurchaseId());
        wrapper.eq("is_delete",0);

        List<DetailPurchaseEntity> entities = mapper.selectList(wrapper);
        List<PurchaseGoodsVOEntity> res = new ArrayList<>();
        for (DetailPurchaseEntity entity1:
                entities) {
            res.add(transfer(entity1));
        }


        return res;
    }
}
